package com.taplinker.client.im.activity.message;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.taplinker.client.AppConfigContext;
import com.taplinker.client.R;
import com.taplinker.client.core.ImageManager;
import com.taplinker.client.im.protocol.TapLinkerConversation;
import com.taplinker.client.im.protocol.TapLinkerMessage;
import com.taplinker.client.im.util.SmileyParser;
import com.taplinker.core.util.ImageUtil;
import com.taplinker.core.util.TimeUtil;

public class LeftTextMessage extends AbstractMessageView{
	private TapLinkerConversation conversation;

	@Override
	public View getMessageView(Context context, TapLinkerMessage message, View convertView) {
		TextMessageHolder textMessageHolder = null;
		if(convertView == null) {
			convertView = LayoutInflater.from(context).inflate(MessageTypeEnum.getMessageType(message).getLayoutId(), null);
			textMessageHolder = new TextMessageHolder();
			textMessageHolder.name = (TextView) convertView.findViewById(R.id.name_text_tv);
			textMessageHolder.head = (ImageView) convertView.findViewById(R.id.head_icon_iv);
			textMessageHolder.content = (TextView) convertView.findViewById(R.id.content_text_tv);
			textMessageHolder.time = (TextView) convertView.findViewById(R.id.time_text_tv);
			convertView.setTag(textMessageHolder);
		} else {
			textMessageHolder = (TextMessageHolder) convertView.getTag();
		}
		String name = message.getSendUser();
//		if (!TextUtils.isEmpty(conversation.getCustomerId())) {
//			// name = name.substring(name.lastIndexOf('_'));
//			name =  message.getSendUser();
//		}
		if (name.length() > 8) {
			name = name.substring(name.lastIndexOf('_') + 1);
		}
		textMessageHolder.name.setText(name);
		if(!TextUtils.isEmpty(message.getIcon())) {
			String headUrl = AppConfigContext.WEB_DISK_ROOT + message.getIcon();
			ImageManager.getInstance().loadImage(headUrl, textMessageHolder.head, ImageUtil.dip2px(context, 45), ImageUtil.dip2px(context, 45),null);
		}
		SmileyParser parser = new SmileyParser(context);
		String content = message.getContent();
		if(!TextUtils.isEmpty(content)) {
			if(content.contains("&nbsp") || content.contains("nbsp")) {
				content = content.replaceAll("?nbsp", "");
			}
		}
		textMessageHolder.content.setText(parser.replace(message.getContent()));
		textMessageHolder.content.setOnLongClickListener(new MessageOnLongClickListener(message, context));
		textMessageHolder.time.setText(TimeUtil.timeFormat(message.getSendTime()));
		return convertView;
	}
	
	class TextMessageHolder {
		TextView name;		// 用户名
		ImageView head;		// 头像
		TextView content;	// 内容
		TextView time;		// 时间
	}

}
